package com.yss.reportworld.controller;

import com.yss.common.core.constant.Constants;
import com.yss.common.core.exception.BusinessException;
import com.yss.common.core.utils.StringUtils;
import com.yss.common.core.web.controller.BaseController;
import com.yss.common.core.web.domain.AjaxResult;
import com.yss.common.core.web.page.TableDataInfo;
import com.yss.common.logger.annotation.Log;
import com.yss.common.logger.enums.BusinessType;
import com.yss.common.redis.service.RedisService;
import com.yss.common.security.annotation.RequiresPermissions;
import com.yss.common.security.utils.DictUtils;
import com.yss.reportworld.domain.kettle.EtlJobTask;
import com.yss.reportworld.enums.ReportWorldEnum;
import com.yss.reportworld.service.ICustomerService;
import com.yss.reportworld.service.ISqlScriptService;
import com.yss.reportworld.service.IEtlJobTaskService;
import com.yss.reportworld.service.IKettleService;
import com.yss.system.api.domain.SysDictData;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * ETL采集Controller
 *
 * @author yss
 * @date 2023-03-29
 */
@RestController
@RequestMapping("/kettle")
public class KettleController extends BaseController {
    @Autowired
    private IKettleService kettleService;
    @Autowired
    private IEtlJobTaskService etlJobTaskService;
    @Autowired
    private ISqlScriptService sqlScriptService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private RedisService redisService;

    /**
     * 查询采集任务列表
     */
    @RequiresPermissions("reportworld:kettle:list")
    @GetMapping("/list")
    public TableDataInfo list(EtlJobTask etlJobTask) {
        startPage();
        List<EtlJobTask> list = etlJobTaskService.selectEtlJobTaskList(etlJobTask);
        return getDataTable(list);
    }

    /**
     * @return
     */
    @RequiresPermissions("reportworld:kettle:list")
    @Log(title = "采集任务", businessType = BusinessType.OTHER)
    @PutMapping("/init")
    public AjaxResult init() {
        AjaxResult ajax = AjaxResult.success();
        // 获取备份的表
        List<SysDictData> sysDictDataList = sqlScriptService.getReductionVersion(false);
        ajax.put("hisVersions", sysDictDataList);
        ajax.put("customerTree", customerService.selectCustomerTreeList());
        return ajax;
    }

    /**
     * 导出采集任务列表
     */
    @RequiresPermissions("reportworld:kettle:export")
    @Log(title = "采集任务", businessType = BusinessType.EXPORT)
    @PostMapping("/downKettleExecl")
    public void export(HttpServletResponse response, EtlJobTask etlJobTask) {
        if (StringUtils.isNull(etlJobTask)) {
            throw new BusinessException("没有获取到要导出的采集任务信息！");
        }
        if (StringUtils.isEmpty(etlJobTask.getIds())) {
            throw new BusinessException("请选择要导出的具体采集任务！");
        }
        if (StringUtils.isEmpty(etlJobTask.getDataBaseType())) {
            throw new BusinessException("请选择要导出的采集任务的数据库类型！");
        }
        kettleService.downKettleExecl(response, etlJobTask);
    }

    /**
     * 读取ETL文件
     */
    @RequiresPermissions("reportworld:kettle:readExecl")
    @Log(title = "采集任务", businessType = BusinessType.IMPORT)
    @PostMapping("/readExecl")
    public AjaxResult readExecl(MultipartFile file, String hisVersion, HttpServletResponse response) throws Exception {
        String sql = kettleService.getKettleSql(file, hisVersion);
        redisService.setCacheObject("ETLSQL", sql);
        return success(sql);
    }

    /**
     * 导出SQL文件
     */
    @RequiresPermissions("reportworld:kettle:readExecl")
    @Log(title = "采集任务", businessType = BusinessType.EXPORT)
    @PostMapping("/handleDownSql")
    public void handleDownSql(HttpServletResponse response) throws IOException {
        if (redisService.hasKey("ETLSQL")) {
            String sql = redisService.getCacheObject("ETLSQL");
            redisService.deleteObject("ETLSQL");
            IOUtils.write(sql, response.getOutputStream(), Constants.UTF8);
        }
    }
}
